package cn.tannn.ops.backup.constant;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 数据库类型
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/9/10 下午12:32
 */
@AllArgsConstructor
@Getter
public enum DbType {
    /**
     * mysql
     */
    MYSQL("MYSQL", "mysql"),
    /**
     * oracle
     */
    ORACLE("ORACLE", "oracle"),
    /**
     * sqlserver
     */
    SQLSERVER("SQLSERVER", "sqlserver"),
    /**
     * postgresql
     */
    POSTGRESQL("POSTGRESQL", "postgresql"),
    ;
    /**
     * 数据库名
     */
    private String name;
    /**
     * 备注
     */
    private String type;

    /**
     * 获取枚举实例
     */
    public static DbType fromName(String name) {
        for (DbType db : DbType.values()) {
            if (Objects.equals(name, db.getName())) {
                return db;
            }
        }
        throw new IllegalArgumentException();
    }

    /**
     * to map
     * @return {name:xx,type:yy}
     */
    public static List<Map<String,String>> toMap(){
        List<Map<String, String>> maps = new ArrayList<>();
        for (DbType value : DbType.values()) {
            maps.add(Map.of("name",value.getName().toUpperCase(),"type",value.getType()));
        }
        return maps;
    }
}
